#ifndef __MY_SERIAL_PORT_H__
#define __MY_SERIAL_PORT_H__
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "reactor.h"

#ifdef USEING_BUFFER
#include "buffer.h"
#endif // USEING_BUFFER


using namespace std;
//typedef function<void(char *)> serialRecvCallback_t;

typedef void(* serialRecvCallback_t)(char *);


class SerialPort
{
public:
    SerialPort(Reactor *user_R,const char *dev_name,int baud_rate);
    ~SerialPort();
    int Send(const char *buf, int len);
    void readHandle(int fd);
    void setRecvCallback(serialRecvCallback_t cb)
    {
        userCallback = cb;
    }
    int enableRecive()
    {
        reactor->addnewchannel(serial_fd,EPOLLIN);
        reactor->setReadableCallback(serial_fd,
                bind(&SerialPort::readHandle, this,std::placeholders::_1));
    }
private:
    int setConfig(int baudrate,char data_bits,char parity_bits,char stop_bits);

private:
    int serial_fd;
    #ifdef USEING_BUFFER
    Buffer *recv_Buf;
    #endif // USEING_BUFFER
    Reactor *reactor;
    serialRecvCallback_t userCallback;//用户回调函数
};


#endif

